<template>
  <div>
    <!-- <a-button @click="handleStateFilter('state_draft')"> draft </a-button> -->
    <a-button @click="handleStateFilter('state_error')"> 导入失败 </a-button>
    <a-button @click="handleStateFilter('')"> 全部 </a-button>
    <a-button @click="handleStateFilter('state_done')"> 导入成功 </a-button>

    <div>
      <!-- 这个模糊搜索不要，这里做示范 -->
      <a-input v-model="filter_name_value"> </a-input>
      <a-button @click="handleNameFilter"> 模糊搜索 name </a-button>
    </div>

    <!-- todo 加一个 按照 用户电话搜索 -->

    <div>
      <!-- 藏起来，如果后面NC导入的数据可以编辑才用 -->
      <!-- <a-button @click="selectSome"> goto NC原始单据 </a-button> -->

      <a-button @click="deleteNC"> 删除 </a-button>
    </div>

    <a-table
      :row-selection="rowSelection"
      :columns="columns"
      :data-source="records"
      rowKey="id"
    >
    </a-table>
  </div>
</template>

<script>
import api from '@/odoorpc'

import simpleTreeMixin from '../simpleTreeMixin'

export default {
  name: 'TreeView',
  components: {},
  mixins: [simpleTreeMixin],
  props: {},

  data() {
    return {
      filter_name_value: ''
    }
  },

  computed: {
    columns() {
      return [
        {
          dataIndex: 'invoice_num',
          key: 'invoice_num',
          title: '发票号',
          ellipsis: true
        },
        {
          dataIndex: 'company_name',
          key: 'company_name',
          title: '公司名称',
          ellipsis: true
        },
        {
          dataIndex: 'user_phone',
          key: 'user_phone',
          title: '用户电话',
          ellipsis: true
        },

        {
          dataIndex: 'partner_code',
          key: 'partner_code',
          title: '客户编码',
          ellipsis: true
        },
        {
          dataIndex: 'partner_name',
          key: 'partner_name',
          title: '客户名称',
          ellipsis: true
        },
        {
          dataIndex: 'amount_total',
          key: 'amount_total',
          title: '总金额',
          ellipsis: true
        },
        {
          dataIndex: 'create_date',
          key: 'create_date',
          title: '创建时间',
          ellipsis: true,
          customRender: value => {
            if (value) {
              // todo // todo 时间 加上时区
              const value2 = value // + 8h
              return value2
            } else {
              return value
            }
          }
        },

        {
          dataIndex: 'state',
          key: 'state',
          title: '状态',

          customRender: value => {
            const meta = this.fields['state']
            const elm = meta.selection.find(item => item[0] === value)
            return elm ? elm[1] : ''
          },

          ellipsis: true
        }
        // { dataIndex: 'api_status', key: 'api_status', title: 'NC状态', ellipsis: true },
        // { dataIndex: 'ods_order_id', key: 'ods_order_id', title: '订单号', ellipsis: true },
        // { dataIndex: 'name', key: 'name2', title: 'NC发票PK', ellipsis: true }
        // { dataIndex: 'biz_type', key: 'biz_type', title: 'NC类型', ellipsis: true },
        // { dataIndex: 'company_code', key: 'company_code', title: '公司代码', ellipsis: true },

        // {
        //   dataIndex: 'action',
        //   key: 'action',
        //   title: '操作',
        //   scopedSlots: { customRender: 'action' }
        // }
      ]
    }
  },

  created() {},

  mounted() {},

  methods: {
    init_filter() {
      // todo 异步分页处理
      this.search_values = this.treeview.search_change(
        this.search_items.group1,
        ['state_error']
      )
    },

    async handleNameFilter() {
      const val = this.filter_name_value

      this.search_values = this.treeview.search_change(this.search_items.name, [
        val
      ])

      this.records = await this.treeview.search_read()
    },

    async handleStateFilter(state_value) {
      this.search_values = this.treeview.search_change(
        this.search_items.group1,
        [state_value]
      )

      this.records = await this.treeview.search_read()
    },

    async _nc2ods(ids) {
      const Model = api.env.model('fp.nc.ods.sale.order')
      const ids2 = await Model.execute('load_to_model', ids)
      return ids2
    },

    async deleteNC() {
      // console.log(this.activeIds)
      await api.env.model('fp.nc.ods.sale.order').unlink(this.activeIds)
      this.search_data()
    },

    async selectSome() {
      //  todo bakeup . 手动处理 draft 的数据
      // const ods_ids = await this._nc2ods(this.activeIds);
      // this.$emit(
      //   'change-menu',
      //   'fp_sale.menu_action_fp_ods_sale_order',
      //   ods_ids
      // )
    }
  }
}
</script>

<style type="text/css"></style>
